<?php
class IndexAction extends CommonAction
{
	public function index()
	{
		 $userInfo = $this->isLogin();
		if ($userInfo) {
		    	$this->redirect("Index/jiekuang");
		 
		}
		cookie("fenxiang", "1", 30);
	   // echo $_SERVER['HTTP_USER_AGENT'];
		$userModel = D("User");
		$userInfo = $this->isLogin();
		$dkmoney = $userModel->getUserDMoney($userInfo["id"]);
		 
		 $this->assign("dkmoney", $dkmoney);
		   
		$info = $userModel->where(array("id" => $userInfo['id']))->find();
		 $doquota=$info['quota']-$dkmoney;
		 
		$this->assign("doquota", $doquota);
		$this->display();
	}
	public function login()
	{
		if ($this->isLogin()) {
			$this->redirect("Index/index");
		}
		$this->display();
	}
    public function reg()
	{
		if ($this->isLogin()) {
			$this->redirect("Index/index");
		}
		$this->display();
	}
	public function forgetpwd()
	{
		if ($this->isPost()) {
			$username = I("username");
			$userInfo = $this->isLogin();
			if ($userInfo) {
				$username = $userInfo["telnum"];
			}
			$password = I("password");
			if (!$username) {
				$this->error("手机号为空");
			}
			if (!isMobile($username)) {
				$this->error("手机号输入不规范");
			}
			if (!$password) {
				$this->error("新密码为空");
			}
			if (strlen($password) < 8 || strlen($password) > 16) {
				$this->error("请输入 8-16 位密码");
			}
			$code = I("code");
			if (strlen($code) != 4) {
				$this->error("短信验证码输入有误");
			}
			$smsModel = D("Sms");
			$sms = $smsModel->getInfo($username, "find");
			if (!$sms) {
				$this->error("短信验证码输入有误");
			}
			if ($sms["send_time"] + 30 * 60 < time()) {
				$this->error("短信验证码失效,请重试");
			}
			$userModel = D("User");
			$password = $userModel->str2pass($password);
			$result = $userModel->getInfo(array("telnum" => $username));
			if (!$result) {
				$this->error("用户不存在");
			}
			$result = $userModel->updateInfo($result["id"], array("password" => $password));
			if (!$result) {
				$this->error("密码找回失败,请稍后再试");
			}
			$this->success("修改成功", U("Index/login"));
		}
		$this->display();
	}
	public function logout()
	{
		$this->setLogin(NULL);
		$this->redirect("Index/index");
		exit(0);
	}
	public function more()
	{
		$this->display();
	}
	public function jiekuang()
	{
	    $userInfo = $this->isLogin();
		if (!$userInfo) {
		    	$this->redirect("Info/login");
		 
		}
		$infoModel = D("Info");
		$infoStatus = $infoModel->getStatus($userInfo["id"]);
		if ($infoStatus == 0) {
		    
		    
	    	$this->redirect("Info/index");
		}
		if ($infoStatus == 0 - 1) {
		    $this->redirect("Loan/refuse");
		 
		}
		if ($infoStatus == 1) {
		    $this->redirect("Loan/review");
		 
		}
		$userAuth = $infoModel->getAuthInfo($userInfo["id"]);
		$idcard = json_decode($userAuth["identity"], true);
	 	$bank = json_decode($userAuth["bank"], true);
	 	$address = json_decode($userAuth["addess"], true);
	 	$contacts = json_decode($userAuth["contacts"], true);
	  	$userModel = D("User");
	   
		   
	  	$info = $userModel->where(array("id" => $userInfo['id']))->find();
	 
	 
		$this->assign("quotao", $info['quota']);
		$this->assign("identity", $idcard);
		$this->assign("bank", $bank);
		$this->assign("address", $address);
		$this->assign("contacts", $contacts);
	    $this->display();
	}
	public function fenxiang()
	{
		$value = cookie("fenxiang");
		if (!$value) {
			$this->redirect("Index/index");
		}
		$this->display();
	}
	public function ajaxLogin()
	{
		$username = I("username");
		$password = I("password");
		if (!$username) {
			$this->error("手机号为空");
		}
		if (!isMobile($username)) {
			$this->error("手机号输入不规范");
		}
		if (!$password) {
			$this->error("密码为空");
		}
		$userModel = D("User");
		$password = $userModel->str2pass($password);
		$result = $userModel->getInfo(array("telnum" => $username, "password" => $password));
		if (!$result) {
			$this->error("用户名或密码有误");
		}
		if (!$result["status"]) {
			$this->error("账户已被禁用,请联系管理员");
		}
		$this->setLogin($result);
		$this->success("登录成功", U("Index/index"));
		return null;
	}
	public function ajaxPsaaWord()
	{
	    $userInfo = $this->isLogin();
		if (!$userInfo) {
		     
		 	$this->success("密码修改失败！", U("Index/Login"));
		}
			$userModel = D("User");
			$password = I("password");
			$password = $userModel->str2pass($password);
			
			$result = $userModel->updateInfo($userInfo["id"], array("password" => $password));
			$this->success("修改成功", U("Index/index"));
	}
	public function ajaxReg()
	{
		$username = I("username");
		$password = I("password");
		if (!$username) {
			$this->error("手机号为空");
		}
		if (!isMobile($username)) {
			$this->error("手机号输入不规范");
		}
		if (!$password) {
			$this->error("密码为空");
		}
		if (strlen($password) < 8 || strlen($password) > 16) {
			$this->error("请输入 8-16 位密码");
		}
		
		$code = I("code");
		 
		if (strlen($code) != 4) {
			$this->error("短信验证码输入有误");
		}
		if (!$code) {
			$this->error("请输入图形验证码");
		}
		//短信开始
		/*
		$smsModel = D("Sms");
		$sms = $smsModel->getInfoMy($username,$code, "reg");
		if (!$sms) {
			$this->error("短信验证码输入有误");
		}
		if ($sms["send_time"] + 30 * 60 < time()) {
			$this->error("短信验证码失效,请重试");
		}
		
		*/
		
		
		 //图形 
		if ($_SESSION["verify"] != md5($code)) {
			$this->error( "图形验证码错误");
		}
		 
		$userModel = D("User");
		$password = $userModel->str2pass($password);
		$result = $userModel->getInfo(array("telnum" => $username));
		if ($result) {
			$this->error("当前手机号已注册,请登录");
		}
		if (!$userModel->addInfo($username, $password)) {
			$this->error("注册失败,请重试");
		}
		$result = $userModel->getInfo(array("telnum" => $username));
		$this->setLogin($result);
		$this->success("注册成功", U("Index/index"));
	}
	public function verify()
	{
		C("app_debug", false);
		import("ORG.Util.Image");
		Image::buildImageVerify();
	}
}